home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / ansi.c < prev    next >
Text File  |  1985-06-03  |  7KB  |  255 lines

  1. /*************************************************************************
  2. main function to test some of the ansi device driving functions
  3. which follow below.
  4.  
  5. written by Rex Jaeschke of Rockville, MD. 1983 (301) 251-8987
  6. compiler used DeSmet v2.2
  7.  
  8. ci () is a DeSmet compiler specific function which does direct console
  9. input of 1 character without echoing it. It is used in sgr_dsr and the
  10. testing program only. All else should run on any compiler.
  11. *************************************************************************/
  12.  
  13. #define CTRL_C 3
  14. #define BELL 7
  15. #define NULL '\0'
  16. #define LINEMIN 1    /* Minimum screen line # */
  17. #define LINEMAX 25    /* Maximum screen line # */
  18. #define COLMMIN 1    /* Minimum screen column # */
  19. #define COLMMAX 80    /* Maximum screen column # */
  20. #define L_ARROW 75
  21. #define R_ARROW 77
  22. #define U_ARROW 72
  23. #define D_ARROW 80
  24. #define HOME 71
  25. #define END 79
  26. #define C_HOME 119
  27.  
  28. main ()
  29. {
  30.     int c;
  31.  
  32.     scr_ed ();            /* clear screen */
  33.     scr_cup (12,40);        /* move to screen center */
  34.     while ((c = ci()) != CTRL_C) {
  35.         if (c == NULL) {    /* do we have extended code? */
  36.             c = ci();
  37.             switch (c) {
  38.             case L_ARROW:
  39.                 scr_cub (1);
  40.                 break;
  41.             case R_ARROW:
  42.                 scr_cuf (1);
  43.                 break;
  44.             case U_ARROW:
  45.                 scr_cuu (1);
  46.                 break;
  47.             case D_ARROW:
  48.                 scr_cud (1);
  49.                 break;
  50.             case C_HOME:
  51.                 scr_ed ();
  52.                 break;
  53.             case HOME:
  54.                 scr_cup (LINEMIN,COLMMIN);
  55.                 break;
  56.             case END:
  57.                 scr_cup (LINEMAX,COLMMAX);
  58.                 break;
  59.             default:
  60.                 putchar (BELL);
  61.                 break;
  62.             }
  63.         }
  64.         else
  65.             putchar (BELL);
  66.     }        
  67. }
  68.  
  69.  
  70. #define ESCAPE 27    /* ASCII ESC character definition */
  71.  
  72. /*************************************************************************
  73. SCR_CUB - Cursor Backward.
  74.  
  75. Moves the cursor backward n columns. Current line remains unchanged. If # of
  76. columns exceeds left-of-screen, cursor is left at the left-most column.
  77. *************************************************************************/
  78.  
  79. scr_cub (ncolms)
  80. int ncolms;
  81. {
  82.     printf ("%c[%dD",ESCAPE,ncolms);
  83. }
  84.  
  85. /*************************************************************************
  86. SCR_CUD - Cursor Down.
  87.  
  88. Moves the cursor down n lines. Current column remains unchanged. If # of lines
  89. to move down exceeds bottom-of-screen, cursor is left at the bottom.
  90. *************************************************************************/
  91.  
  92. scr_cud (nlines)
  93. int nlines;
  94. {
  95.     printf ("%c[%dB",ESCAPE,nlines);
  96. }
  97.  
  98. /*************************************************************************
  99. SCR_CUF - Cursor Forward.
  100.  
  101. Moves the cursor forward n columns. Current line remains unchanged. If # of
  102. columns exceeds right-of-screen, cursor is left at the right-most column.
  103. *************************************************************************/
  104.  
  105. scr_cuf (ncolms)
  106. int ncolms;
  107. {
  108.     printf ("%c[%dC",ESCAPE,ncolms);
  109. }
  110.  
  111. /*************************************************************************
  112. SCR_CUP - Cursor Position. (same as HVP)
  113.  
  114. Moves the cursor to the specified position line,colm.
  115. *************************************************************************/
  116.  
  117. scr_cup (line,colm)
  118. int line,colm;
  119. {
  120.     printf ("%c[%d;%dH",ESCAPE,line,colm);
  121. }
  122.  
  123. /*************************************************************************
  124. SCR_CUU - Cursor Up.
  125.  
  126. Moves the cursor up n lines. Current column remains unchanged. If # of lines
  127. to move up exceeds top-of-screen, cursor is left at the top.
  128. *************************************************************************/
  129.  
  130. scr_cuu (nlines)
  131. int nlines;
  132. {
  133.     printf ("%c[%dA",ESCAPE,nlines);
  134. }
  135.  
  136. /*************************************************************************
  137. SCR_DSR - Device Status Report.
  138.  
  139. Returns the Cursor Position Report (CPR) sequence in the form ESC[line;colmR
  140.  
  141. ci () is a DeSmet compiler specific function which does direct console
  142. input of 1 character without echoing it.
  143. *************************************************************************/
  144.  
  145. scr_dsr (line,colm)
  146. int *line,*colm;
  147. {
  148.     int i = 0;
  149.     char cpr[10];
  150.  
  151.     printf ("%c[6n",ESCAPE);
  152.     while ((cpr[i++] = ci ()) != 'R')
  153.         ;
  154.     cpr[i] = '\0';
  155.  
  156. /* format of cpr[] is ESC[rr;ccR row and colm are always two digits */
  157.  
  158.     *line = ((cpr[2]-'0')*10)+cpr[3]-'0';
  159.     *colm = ((cpr[5]-'0')*10)+cpr[6]-'0';
  160. }
  161.  
  162. /*************************************************************************
  163. SCR_ED - Erase in Display.
  164.  
  165. Erases all of the screen leaving the cursor at home
  166. *************************************************************************/
  167.  
  168. scr_ed ()
  169. {
  170.     printf ("%c[2J",ESCAPE);
  171. }
  172.  
  173. /*************************************************************************
  174. SCR_EL - Erase in Line.
  175.  
  176. Erases from the cursor to the end of the line including the cursor position.
  177. *************************************************************************/
  178.  
  179. scr_el ()
  180. {
  181.     printf ("%c[2K",ESCAPE);
  182. }
  183.  
  184. /*************************************************************************
  185. SCR_HVP - Horizontal and Vertical Position. (same as CUP)
  186.  
  187. Moves the cursor to the specified position line,colm.
  188. *************************************************************************/
  189.  
  190. scr_hvp (line,colm)
  191. int line,colm;
  192. {
  193.     printf ("%c[%d;%dH",ESCAPE,line,colm);
  194. }
  195.  
  196. /*************************************************************************
  197. SCR_RCP - Restore Cursor Position.
  198.  
  199. Restores the cursor to the value it had when previously saved by scr_scp.
  200. *************************************************************************/
  201.  
  202. scr_rcp ()
  203. {
  204.     printf ("%c[u",ESCAPE);
  205. }
  206.  
  207. /*************************************************************************
  208. SCR_SCP - Save Cursor Position.
  209.  
  210. Saves the current cursor position for later restoration by scr_rcp.
  211. *************************************************************************/
  212.  
  213. scr_scp ()
  214. {
  215.     printf ("%c[s",ESCAPE);
  216. }
  217.  
  218. /*************************************************************************
  219. SCR_SGR - Set Graphics Rendition.
  220.  
  221. Sets the character attribute specified by the parameter.
  222. Attributes remain in force until reset or changed.
  223. *************************************************************************/
  224.  
  225. scr_sgr (attrib)
  226. int attrib;
  227. {
  228.     printf ("%c[%dm",ESCAPE,attrib);
  229. }
  230.  
  231. /*************************************************************************
  232. SCR_SM - Set Mode.
  233.  
  234. Sets the screen width or type specified by the parameter.
  235. *************************************************************************/
  236.  
  237. scr_sm (param)
  238. int param;
  239. {
  240.     printf ("%c[=%dh",ESCAPE,param);
  241. }
  242.  
  243. /*************************************************************************
  244. SCR_RM - Reset Mode.
  245.  
  246. Sets the screen width or type specified by the parameter.
  247. *************************************************************************/
  248.  
  249. scr_rm (param)
  250. int param;
  251. {
  252.     printf ("%c[=%dl",ESCAPE,param);
  253. }
  254. *****************************************************/
  255.